package cn.sh.yhk.oracle;

import cn.sh.yhk.oracle.TTeamSegExample.Criteria;
import cn.sh.yhk.oracle.TTeamSegExample.Criterion;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public class TTeamSegSqlProvider {

    public String countByExample(TTeamSegExample example) {
        SQL sql = new SQL();
        sql.SELECT("count(*)").FROM("T_TEAM_SEG");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String deleteByExample(TTeamSegExample example) {
        SQL sql = new SQL();
        sql.DELETE_FROM("T_TEAM_SEG");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String insertSelective(TTeamSeg record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("T_TEAM_SEG");
        
        if (record.getSegId() != null) {
            sql.VALUES("SEG_ID", "#{segId,jdbcType=DECIMAL}");
        }
        
        if (record.getTeamId() != null) {
            sql.VALUES("TEAM_ID", "#{teamId,jdbcType=DECIMAL}");
        }
        
        if (record.getSeggroupId() != null) {
            sql.VALUES("SEGGROUP_ID", "#{seggroupId,jdbcType=DECIMAL}");
        }
        
        if (record.getCreateDtm() != null) {
            sql.VALUES("CREATE_DTM", "#{createDtm,jdbcType=DATE}");
        }
        
        if (record.getCreateId() != null) {
            sql.VALUES("CREATE_ID", "#{createId,jdbcType=DECIMAL}");
        }
        
        if (record.getSegSq() != null) {
            sql.VALUES("SEG_SQ", "#{segSq,jdbcType=DECIMAL}");
        }
        
        if (record.getDepCd() != null) {
            sql.VALUES("DEP_CD", "#{depCd,jdbcType=VARCHAR}");
        }
        
        if (record.getArrCd() != null) {
            sql.VALUES("ARR_CD", "#{arrCd,jdbcType=VARCHAR}");
        }
        
        if (record.getDepDt() != null) {
            sql.VALUES("DEP_DT", "#{depDt,jdbcType=DATE}");
        }
        
        if (record.getArrDt() != null) {
            sql.VALUES("ARR_DT", "#{arrDt,jdbcType=DATE}");
        }
        
        if (record.getPlanetp() != null) {
            sql.VALUES("PLANETP", "#{planetp,jdbcType=VARCHAR}");
        }
        
        if (record.getStop() != null) {
            sql.VALUES("STOP", "#{stop,jdbcType=DECIMAL}");
        }
        
        if (record.getSegTp() != null) {
            sql.VALUES("SEG_TP", "#{segTp,jdbcType=DECIMAL}");
        }
        
        if (record.getFaresourceid() != null) {
            sql.VALUES("FARESOURCEID", "#{faresourceid,jdbcType=VARCHAR}");
        }
        
        if (record.getFareTp() != null) {
            sql.VALUES("FARE_TP", "#{fareTp,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightNo() != null) {
            sql.VALUES("FLIGHT_NO", "#{flightNo,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightGroupId() != null) {
            sql.VALUES("FLIGHT_GROUP_ID", "#{flightGroupId,jdbcType=DECIMAL}");
        }
        
        if (record.getRealflight() != null) {
            sql.VALUES("REALFLIGHT", "#{realflight,jdbcType=VARCHAR}");
        }
        
        if (record.getAdtFuelTax() != null) {
            sql.VALUES("ADT_FUEL_TAX", "#{adtFuelTax,jdbcType=DECIMAL}");
        }
        
        if (record.getAdtAirportTax() != null) {
            sql.VALUES("ADT_AIRPORT_TAX", "#{adtAirportTax,jdbcType=DECIMAL}");
        }
        
        if (record.getJinkunSegId() != null) {
            sql.VALUES("JINKUN_SEG_ID", "#{jinkunSegId,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightPlanId() != null) {
            sql.VALUES("FLIGHT_PLAN_ID", "#{flightPlanId,jdbcType=VARCHAR}");
        }
        
        if (record.getJinkunLanId() != null) {
            sql.VALUES("JINKUN_LAN_ID", "#{jinkunLanId,jdbcType=VARCHAR}");
        }
        
        if (record.getAssembledTp() != null) {
            sql.VALUES("ASSEMBLED_TP", "#{assembledTp,jdbcType=OTHER}");
        }
        
        if (record.getSegCabin() != null) {
            sql.VALUES("SEG_CABIN", "#{segCabin,jdbcType=VARCHAR}");
        }
        
        if (record.getTravelType() != null) {
            sql.VALUES("TRAVEL_TYPE", "#{travelType,jdbcType=VARCHAR}");
        }
        
        if (record.getDepTerm() != null) {
            sql.VALUES("DEP_TERM", "#{depTerm,jdbcType=VARCHAR}");
        }
        
        if (record.getArriTerm() != null) {
            sql.VALUES("ARRI_TERM", "#{arriTerm,jdbcType=VARCHAR}");
        }
        
        return sql.toString();
    }

    public String selectByExample(TTeamSegExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("SEG_ID");
        } else {
            sql.SELECT("SEG_ID");
        }
        sql.SELECT("TEAM_ID");
        sql.SELECT("SEGGROUP_ID");
        sql.SELECT("CREATE_DTM");
        sql.SELECT("CREATE_ID");
        sql.SELECT("SEG_SQ");
        sql.SELECT("DEP_CD");
        sql.SELECT("ARR_CD");
        sql.SELECT("DEP_DT");
        sql.SELECT("ARR_DT");
        sql.SELECT("PLANETP");
        sql.SELECT("STOP");
        sql.SELECT("SEG_TP");
        sql.SELECT("FARESOURCEID");
        sql.SELECT("FARE_TP");
        sql.SELECT("FLIGHT_NO");
        sql.SELECT("FLIGHT_GROUP_ID");
        sql.SELECT("REALFLIGHT");
        sql.SELECT("ADT_FUEL_TAX");
        sql.SELECT("ADT_AIRPORT_TAX");
        sql.SELECT("JINKUN_SEG_ID");
        sql.SELECT("FLIGHT_PLAN_ID");
        sql.SELECT("JINKUN_LAN_ID");
        sql.SELECT("ASSEMBLED_TP");
        sql.SELECT("SEG_CABIN");
        sql.SELECT("TRAVEL_TYPE");
        sql.SELECT("DEP_TERM");
        sql.SELECT("ARRI_TERM");
        sql.FROM("T_TEAM_SEG");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        TTeamSeg record = (TTeamSeg) parameter.get("record");
        TTeamSegExample example = (TTeamSegExample) parameter.get("example");
        
        SQL sql = new SQL();
        sql.UPDATE("T_TEAM_SEG");
        
        if (record.getSegId() != null) {
            sql.SET("SEG_ID = #{record.segId,jdbcType=DECIMAL}");
        }
        
        if (record.getTeamId() != null) {
            sql.SET("TEAM_ID = #{record.teamId,jdbcType=DECIMAL}");
        }
        
        if (record.getSeggroupId() != null) {
            sql.SET("SEGGROUP_ID = #{record.seggroupId,jdbcType=DECIMAL}");
        }
        
        if (record.getCreateDtm() != null) {
            sql.SET("CREATE_DTM = #{record.createDtm,jdbcType=DATE}");
        }
        
        if (record.getCreateId() != null) {
            sql.SET("CREATE_ID = #{record.createId,jdbcType=DECIMAL}");
        }
        
        if (record.getSegSq() != null) {
            sql.SET("SEG_SQ = #{record.segSq,jdbcType=DECIMAL}");
        }
        
        if (record.getDepCd() != null) {
            sql.SET("DEP_CD = #{record.depCd,jdbcType=VARCHAR}");
        }
        
        if (record.getArrCd() != null) {
            sql.SET("ARR_CD = #{record.arrCd,jdbcType=VARCHAR}");
        }
        
        if (record.getDepDt() != null) {
            sql.SET("DEP_DT = #{record.depDt,jdbcType=DATE}");
        }
        
        if (record.getArrDt() != null) {
            sql.SET("ARR_DT = #{record.arrDt,jdbcType=DATE}");
        }
        
        if (record.getPlanetp() != null) {
            sql.SET("PLANETP = #{record.planetp,jdbcType=VARCHAR}");
        }
        
        if (record.getStop() != null) {
            sql.SET("STOP = #{record.stop,jdbcType=DECIMAL}");
        }
        
        if (record.getSegTp() != null) {
            sql.SET("SEG_TP = #{record.segTp,jdbcType=DECIMAL}");
        }
        
        if (record.getFaresourceid() != null) {
            sql.SET("FARESOURCEID = #{record.faresourceid,jdbcType=VARCHAR}");
        }
        
        if (record.getFareTp() != null) {
            sql.SET("FARE_TP = #{record.fareTp,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightNo() != null) {
            sql.SET("FLIGHT_NO = #{record.flightNo,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightGroupId() != null) {
            sql.SET("FLIGHT_GROUP_ID = #{record.flightGroupId,jdbcType=DECIMAL}");
        }
        
        if (record.getRealflight() != null) {
            sql.SET("REALFLIGHT = #{record.realflight,jdbcType=VARCHAR}");
        }
        
        if (record.getAdtFuelTax() != null) {
            sql.SET("ADT_FUEL_TAX = #{record.adtFuelTax,jdbcType=DECIMAL}");
        }
        
        if (record.getAdtAirportTax() != null) {
            sql.SET("ADT_AIRPORT_TAX = #{record.adtAirportTax,jdbcType=DECIMAL}");
        }
        
        if (record.getJinkunSegId() != null) {
            sql.SET("JINKUN_SEG_ID = #{record.jinkunSegId,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightPlanId() != null) {
            sql.SET("FLIGHT_PLAN_ID = #{record.flightPlanId,jdbcType=VARCHAR}");
        }
        
        if (record.getJinkunLanId() != null) {
            sql.SET("JINKUN_LAN_ID = #{record.jinkunLanId,jdbcType=VARCHAR}");
        }
        
        if (record.getAssembledTp() != null) {
            sql.SET("ASSEMBLED_TP = #{record.assembledTp,jdbcType=OTHER}");
        }
        
        if (record.getSegCabin() != null) {
            sql.SET("SEG_CABIN = #{record.segCabin,jdbcType=VARCHAR}");
        }
        
        if (record.getTravelType() != null) {
            sql.SET("TRAVEL_TYPE = #{record.travelType,jdbcType=VARCHAR}");
        }
        
        if (record.getDepTerm() != null) {
            sql.SET("DEP_TERM = #{record.depTerm,jdbcType=VARCHAR}");
        }
        
        if (record.getArriTerm() != null) {
            sql.SET("ARRI_TERM = #{record.arriTerm,jdbcType=VARCHAR}");
        }
        
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("T_TEAM_SEG");
        
        sql.SET("SEG_ID = #{record.segId,jdbcType=DECIMAL}");
        sql.SET("TEAM_ID = #{record.teamId,jdbcType=DECIMAL}");
        sql.SET("SEGGROUP_ID = #{record.seggroupId,jdbcType=DECIMAL}");
        sql.SET("CREATE_DTM = #{record.createDtm,jdbcType=DATE}");
        sql.SET("CREATE_ID = #{record.createId,jdbcType=DECIMAL}");
        sql.SET("SEG_SQ = #{record.segSq,jdbcType=DECIMAL}");
        sql.SET("DEP_CD = #{record.depCd,jdbcType=VARCHAR}");
        sql.SET("ARR_CD = #{record.arrCd,jdbcType=VARCHAR}");
        sql.SET("DEP_DT = #{record.depDt,jdbcType=DATE}");
        sql.SET("ARR_DT = #{record.arrDt,jdbcType=DATE}");
        sql.SET("PLANETP = #{record.planetp,jdbcType=VARCHAR}");
        sql.SET("STOP = #{record.stop,jdbcType=DECIMAL}");
        sql.SET("SEG_TP = #{record.segTp,jdbcType=DECIMAL}");
        sql.SET("FARESOURCEID = #{record.faresourceid,jdbcType=VARCHAR}");
        sql.SET("FARE_TP = #{record.fareTp,jdbcType=VARCHAR}");
        sql.SET("FLIGHT_NO = #{record.flightNo,jdbcType=VARCHAR}");
        sql.SET("FLIGHT_GROUP_ID = #{record.flightGroupId,jdbcType=DECIMAL}");
        sql.SET("REALFLIGHT = #{record.realflight,jdbcType=VARCHAR}");
        sql.SET("ADT_FUEL_TAX = #{record.adtFuelTax,jdbcType=DECIMAL}");
        sql.SET("ADT_AIRPORT_TAX = #{record.adtAirportTax,jdbcType=DECIMAL}");
        sql.SET("JINKUN_SEG_ID = #{record.jinkunSegId,jdbcType=VARCHAR}");
        sql.SET("FLIGHT_PLAN_ID = #{record.flightPlanId,jdbcType=VARCHAR}");
        sql.SET("JINKUN_LAN_ID = #{record.jinkunLanId,jdbcType=VARCHAR}");
        sql.SET("ASSEMBLED_TP = #{record.assembledTp,jdbcType=OTHER}");
        sql.SET("SEG_CABIN = #{record.segCabin,jdbcType=VARCHAR}");
        sql.SET("TRAVEL_TYPE = #{record.travelType,jdbcType=VARCHAR}");
        sql.SET("DEP_TERM = #{record.depTerm,jdbcType=VARCHAR}");
        sql.SET("ARRI_TERM = #{record.arriTerm,jdbcType=VARCHAR}");
        
        TTeamSegExample example = (TTeamSegExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(TTeamSeg record) {
        SQL sql = new SQL();
        sql.UPDATE("T_TEAM_SEG");
        
        if (record.getTeamId() != null) {
            sql.SET("TEAM_ID = #{teamId,jdbcType=DECIMAL}");
        }
        
        if (record.getSeggroupId() != null) {
            sql.SET("SEGGROUP_ID = #{seggroupId,jdbcType=DECIMAL}");
        }
        
        if (record.getCreateDtm() != null) {
            sql.SET("CREATE_DTM = #{createDtm,jdbcType=DATE}");
        }
        
        if (record.getCreateId() != null) {
            sql.SET("CREATE_ID = #{createId,jdbcType=DECIMAL}");
        }
        
        if (record.getSegSq() != null) {
            sql.SET("SEG_SQ = #{segSq,jdbcType=DECIMAL}");
        }
        
        if (record.getDepCd() != null) {
            sql.SET("DEP_CD = #{depCd,jdbcType=VARCHAR}");
        }
        
        if (record.getArrCd() != null) {
            sql.SET("ARR_CD = #{arrCd,jdbcType=VARCHAR}");
        }
        
        if (record.getDepDt() != null) {
            sql.SET("DEP_DT = #{depDt,jdbcType=DATE}");
        }
        
        if (record.getArrDt() != null) {
            sql.SET("ARR_DT = #{arrDt,jdbcType=DATE}");
        }
        
        if (record.getPlanetp() != null) {
            sql.SET("PLANETP = #{planetp,jdbcType=VARCHAR}");
        }
        
        if (record.getStop() != null) {
            sql.SET("STOP = #{stop,jdbcType=DECIMAL}");
        }
        
        if (record.getSegTp() != null) {
            sql.SET("SEG_TP = #{segTp,jdbcType=DECIMAL}");
        }
        
        if (record.getFaresourceid() != null) {
            sql.SET("FARESOURCEID = #{faresourceid,jdbcType=VARCHAR}");
        }
        
        if (record.getFareTp() != null) {
            sql.SET("FARE_TP = #{fareTp,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightNo() != null) {
            sql.SET("FLIGHT_NO = #{flightNo,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightGroupId() != null) {
            sql.SET("FLIGHT_GROUP_ID = #{flightGroupId,jdbcType=DECIMAL}");
        }
        
        if (record.getRealflight() != null) {
            sql.SET("REALFLIGHT = #{realflight,jdbcType=VARCHAR}");
        }
        
        if (record.getAdtFuelTax() != null) {
            sql.SET("ADT_FUEL_TAX = #{adtFuelTax,jdbcType=DECIMAL}");
        }
        
        if (record.getAdtAirportTax() != null) {
            sql.SET("ADT_AIRPORT_TAX = #{adtAirportTax,jdbcType=DECIMAL}");
        }
        
        if (record.getJinkunSegId() != null) {
            sql.SET("JINKUN_SEG_ID = #{jinkunSegId,jdbcType=VARCHAR}");
        }
        
        if (record.getFlightPlanId() != null) {
            sql.SET("FLIGHT_PLAN_ID = #{flightPlanId,jdbcType=VARCHAR}");
        }
        
        if (record.getJinkunLanId() != null) {
            sql.SET("JINKUN_LAN_ID = #{jinkunLanId,jdbcType=VARCHAR}");
        }
        
        if (record.getAssembledTp() != null) {
            sql.SET("ASSEMBLED_TP = #{assembledTp,jdbcType=OTHER}");
        }
        
        if (record.getSegCabin() != null) {
            sql.SET("SEG_CABIN = #{segCabin,jdbcType=VARCHAR}");
        }
        
        if (record.getTravelType() != null) {
            sql.SET("TRAVEL_TYPE = #{travelType,jdbcType=VARCHAR}");
        }
        
        if (record.getDepTerm() != null) {
            sql.SET("DEP_TERM = #{depTerm,jdbcType=VARCHAR}");
        }
        
        if (record.getArriTerm() != null) {
            sql.SET("ARRI_TERM = #{arriTerm,jdbcType=VARCHAR}");
        }
        
        sql.WHERE("SEG_ID = #{segId,jdbcType=DECIMAL}");
        
        return sql.toString();
    }

    protected void applyWhere(SQL sql, TTeamSegExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }
}